$(document).ready(function() {
    dataSource = new kendo.data.DataSource({
        transport: {
            create: {
                url: appBosch.crud.create,
                dataType: "json"
            },
            read: {
                url: appBosch.crud.read,
                dataType: "json"
            },
            update: {
                url: appBosch.crud.update,
                dataType: "json"
            },
            destroy: {
                url: appBosch.crud.destroy,
                dataType: "json"
            },
            parameterMap: function(options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'create':
                        params = {models: kendo.stringify(options.models)};
                    case 'read':
                        break;
                    case 'update':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'destroy':
                        params = {id: options.models[0].id};
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id",
                fields: {
                    source: {
                        editable: false,
                        nullable: false,
                        defaultValue: appBosch.workstationConfigId
                    },
                    destination: {
                        editable: true,
                        nullable: false,
                        defaultValue: appBosch.workstationsConfigs[0].value,
                        validation: {
                            required: true
                        }
                    }
                }
            }
        }
    });

    $("#grid").kendoGrid({
        scrollable: true,
        resizable: true,
        columnMenu: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        dataSource: dataSource,
        toolbar: kendo.template($("#templateToolbar").html()),
        columns: [
            {field: "destination", title: "Dependencia", values: appBosch.workstationsConfigs, width: 700},
            {command: [
                    {
                        name: "edit",
                        text: {
                            edit: "Editar",
                            update: "Actualizar",
                            cancel: "Cancelar"
                        }
                    },
                    {
                        name: "destroy",
                        text: "Eliminar"
                    }], title: "&nbsp;"}],
        editable: {
            confirmation: "¿Estas seguro que deseas eliminar este tema?",
            mode: "popup"
        }
    });
});

function showLinks(e)
{
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var workstationConfigId = dataItem.id;
    var parts = [appBosch.urlConfig, workstationConfigId, appBosch.workstationId, appBosch.lineId, appBosch.areaId, appBosch.moeId];
    var url = parts.join('/');
    $(location).attr('href', url);
}